package com.example.demo;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class StreamDemo {
    public static void main(String[] args) {
        List<Person> personList = Arrays.asList(
                new Person(1, "大毛", 30, 175),
                new Person(2, "二毛", 35, 170),
                new Person(3, "三毛", 26, 173),
                new Person(4, "小毛", 30, 175));

        // 获取 person 类型流
        Stream<Person> personStream = personList.stream();
        // 按照年龄升序排序,然后获取排序后的最大值 o1-o2 代表升序排序;o2-o1 代表降序排序
        Optional<Person> max = personStream.max(((o1, o2) -> o1.getAge() - o2.getAge()));
        System.out.println(max.get().age);

    }
}
class Person{
    int id;
    String name;
    int age;
    int height;

    public Person(int id,String name,int age,int height){
        this.id=id;
        this.name = name;
        this.age = age;
        this.height=height;
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}